package com.sxt.system.service.impl;

import com.sxt.system.constast.Constast;
import com.sxt.system.domain.Permission;
import com.sxt.system.mapper.PermissionMapper;
import com.sxt.system.mapper.RoleMapper;
import com.sxt.system.service.IPermissionService;
import com.sxt.system.utils.DataGridView;
import com.sxt.system.utils.TreeNode;
import com.sxt.system.vo.PermissionVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 俊哥
 * @since 2019-11-09
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

	private Log log = LogFactory.getLog(PermissionServiceImpl.class);
	
	@Autowired
	private RoleMapper roleMapper;
	
	@Override
	public List<Permission> queryAllPermissionForList(PermissionVO permissionVO) {
		PermissionMapper permissionMapper = this.getBaseMapper();
		QueryWrapper<Permission> qw = new QueryWrapper<Permission>();
		if(null != permissionVO) {
			qw.eq(StringUtils.isNotBlank(permissionVO.getType()), "type", permissionVO.getType());
			qw.eq(permissionVO.getAvailable() != null, "available", permissionVO.getAvailable());
			qw.like(StringUtils.isNoneBlank(permissionVO.getTitle()), "title", permissionVO.getTitle());
		}else {
			log.info("permissionVO的参数为空");
		}
		qw.orderByAsc("ordernum");
		List<Permission> list = permissionMapper.selectList(qw);
		return list;
	}

	@Override
	public DataGridView loadAllPermission(PermissionVO permissionVO) {
		Page<Permission> page = new Page<>(permissionVO.getPage(),permissionVO.getLimit());
		
		QueryWrapper<Permission> qw = new QueryWrapper<Permission>();
		qw.eq(StringUtils.isNoneBlank(permissionVO.getType()), "type", permissionVO.getType());
		qw.eq(permissionVO.getAvailable()!= null, "available", permissionVO.getAvailable());
		qw.like(StringUtils.isNoneBlank(permissionVO.getTitle()), "title", permissionVO.getTitle());
		qw.like(StringUtils.isNoneBlank(permissionVO.getPercode()), "percode", permissionVO.getPercode());
		//里面可以并行条件  =相当于and(aaa=xxx or aaaa=xxxx)
		qw.and(permissionVO.getId()!=null, new Consumer<QueryWrapper<Permission>>() {
			
			@Override
			public void accept(QueryWrapper<Permission> t) {
				t.eq(permissionVO.getId()!=null, "id", permissionVO.getId()).or().
				eq(permissionVO.getId()!=null, "pid", permissionVO.getId());
			}
		});
		qw.orderByAsc("ordernum");
		this.getBaseMapper().selectPage(page, qw);
		return new DataGridView(page.getTotal(), page.getRecords());
	}

	@Override
	public Integer queryPermissionMaxOrderNum() {
		return this.getBaseMapper().queryPermissionMaxOrderNum();
	}

	@Override
	public Permission addPermission(Permission permission) {
		this.baseMapper.insert(permission);
		return permission;
	}

	@Override
	public Permission updatePermission(Permission permission) {
		this.baseMapper.updateById(permission);
		return permission;
	}

	@Override
	public Integer queryPermissionCountByPid(Integer id) {
		return this.baseMapper.queryPermissionCountByPid(id);
	}

	@Override
	public boolean removeById(Serializable id) {
		//根据菜单或权限ID删除sys_role_permission的数据
		this.roleMapper.deleteRolePermissionByPid(id);
		return super.removeById(id);
	}

	@Override
	public DataGridView queryRolePermissionByRoleId(Integer roleId) {
		QueryWrapper<Permission> qw = new QueryWrapper<>();
		qw.eq("available", Constast.AVAILABLE_TRUE);
		//1查询所有的菜单和权限
		List<Permission> allPermissin = this.baseMapper.selectList(qw);
		//2根据角色ID查询当前角色拥有的权限ID的集合
		List<Integer> permissionIds = this.baseMapper.queryPermissionIdsByRoleId(roleId);
		List<Permission> currRolePermission = null;
		//3.根据角色ID查询当前角色拥有的权限集合
		if(permissionIds == null || permissionIds.size() == 0) {
			currRolePermission = new ArrayList<Permission>();
		}else {//有权限
			qw.in("id", permissionIds);
			currRolePermission = this.baseMapper.selectList(qw);
		}
		List<TreeNode> nodes = new ArrayList<TreeNode>();
		//判断是否选中
		for (Permission p1 : allPermissin) {
			String checkArr="0";
			for (Permission p2 : currRolePermission) {
				if(p1.getId()==p2.getId()) {
					checkArr="1";
					break;
				}
			}
			Boolean spread = p1.getOpen()==null?false:p1.getOpen()==1?true:false;
			nodes.add(new TreeNode(p1.getId(), p1.getPid(), p1.getTitle(), spread, checkArr));
		}
		
		return new DataGridView(nodes);
	}

	@Override
	public List<Permission> queryPermissionByUserIdForList(PermissionVO permissionVO, Integer id) {
		//根据用户ID查询用户的角色ID
		List<Integer> roleIds = this.roleMapper.selectRoleIdsByUserId(id);
		if(null == roleIds||roleIds.size() == 0) {
			return null;
		}else{
			//根据角色ID查询权限ID
			List<Integer> permissionIds = this.getBaseMapper().queryPermissionIdsByRoleIds(roleIds);
			if(null == permissionIds || permissionIds.size() == 0) {
				return null;
			}else {
				QueryWrapper<Permission> qw = new QueryWrapper<Permission>();
				qw.eq("available", Constast.AVAILABLE_TRUE);
				qw.eq("type", Constast.TYPE_MENU);
				qw.in("id", permissionIds);
				List<Permission> permissionObjs = this.getBaseMapper().selectList(qw);
				return permissionObjs;
			}
		}
	}

	/**
	 * 查询用户拥有的权限编码
	 */
	@Override
	public List<String> queryPermissionsByUserId(Integer id) {
		//根据用户ID查询角色ID
		List<Integer> roleIds = this.roleMapper.selectRoleIdsByUserId(id);
		if(null == roleIds || roleIds.size() == 0) {
			return null;
		}else {
			//根据角色ID集合查询权限ID
			List<Integer> permissionIds = this.getBaseMapper().queryPermissionIdsByRoleIds(roleIds);
			if(null == permissionIds || permissionIds.size() == 0) {
				return null;
			}else {
				QueryWrapper<Permission> qw = new QueryWrapper<Permission>();
				qw.eq("available", Constast.AVAILABLE_TRUE);
				qw.eq("type", Constast.TYPE_PERMISSION);
				qw.eq("id", permissionIds);
				List<Permission> permissionObjs = this.getBaseMapper().selectList(qw);
				//根据用户ID查询权限
				List<String> permissions = new ArrayList<String>();
				for (Permission permission : permissionObjs) {
					permissions.add(permission.getPercode());
				}
				return permissions;
			}
		}
	}
}
